<!--
  @name: 用户管理
  @author: lizibin
 -->
<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      :request-api="getTableList"
      :init-param="initParam"
      :data-callback="dataCallback"
      @darg-sort="sortTable"
    >
      <!-- 表格 header 按钮 -->
      <!-- <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增</el-button>
      </template> -->
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="success" link :icon="Delete" @click="openDrawer('', scope.row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" v-if="scope.row.userType !== 0" @click="deleteAccount(scope.row)"
          >删除</el-button
        >
      </template>
    </ProTable>
    <UserDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import SvgIcon from "@/components/SvgIcon/index.vue";
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { getUser, deleteUser, updateUser } from "@/api/modules/allApi";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { ElMessage, ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import UserDrawer from "@/views/proTable/components/UserDrawer.vue";
import { ProTableInstance, ColumnProps, HeaderRenderScope } from "@/components/ProTable/interface";

const router = useRouter();

// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({ type: 1 });

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  newParams.userType = 0;
  newParams.currentPage = newParams.pageNum;
  newParams.userId = 1;
  delete newParams.endTime;
  delete newParams.startTime;
  delete newParams.pageNum;
  return getUser(newParams);
};

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { prop: "userName", label: "用户名" },
  { prop: "userEmail", label: "用户邮箱" },
  {
    prop: "userType",
    label: "用户类型",
    render: scope => {
      return <div>{scope.row.userType == 0 ? "管理员" : "普通用户"}</div>;
    }
  },
  {
    prop: "userIcon",
    label: "头像",
    render: scope => {
      return (
        <div>
          <el-image
            style="width: 100px; height: 100px"
            src={import.meta.env.VITE_API_URL + scope.row.userIcon}
            zoom-rate="1.2"
            max-scale="7"
            min-scale="0.2"
            initial-index="4"
            preview-src-list={[import.meta.env.VITE_API_URL + scope.row.userIcon]}
            fit="cover"
          />
        </div>
      );
    }
  },
  { prop: "userSignature", label: "签名" },
  { prop: "userRegdate", label: "注册时间" },
  { prop: "operation", label: "操作", fixed: "right", width: 200 }
]);

// 删除信息
const deleteAccount = async (params: any) => {
  await useHandleData(deleteUser, { userId: params.userId }, `删除【${params.userName}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<any> | null>(null);
const openDrawer = (title: string, row: Partial<any> = {}) => {
  row.userType = row.userType + "";
  const params = {
    title,
    isView: false,
    row: { ...row },
    api: updateUser,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
<style lang="scss">
.sty_link {
  color: rgb(47 167 185);
}
</style>
